#!/bin/bash

work_dir=""
name="rs_u20_ros_docker"
docker_image="rs:ros-arm-u20-xavier"
container_env="${name}_env"

error() {
    local date=$(date)
    local para=$1
    echo -e "\033[31m[$date] error : $para \033[0m"
}

info() {
    local date=$(date)
    local para=$1
    echo -e "\033[32m[$date] info : $para \033[0m"
}

fun_do_help() {
    echo "usage: $0 [-r / --run] [-s / --stop] [-e / --exce] [-k / --kill <name>] [--remove]"
    echo "  [-r / --run] 运行容器，支持容器其他的运行参数,使用++替代--"
    echo "  [-s / --stop] 停止容器，支持传入容器的名称，默认名称 : $name"
    echo "  [-e / --exce] 进入容器，支持传入容器的名称，默认名称 : $name"
    echo "  [-k / --kill] 杀死容器，支持传入容器的名称，默认名称 : $name"
    echo "  [--remove] 删除容器，支持传入容器的名称，默认名称 : $name"
    echo "  eg:"
    echo "      $0"
    echo "      $0 -r"
    echo "      $0 --run"
    echo "      $0 -r +[容器参数] ++[容器参数]=xxx"
    echo "      $0 -e"
    echo "      $0 -exce"
    echo "      $0 -s"
    echo "      $0 -stop"
    echo "      $0 -k"
    echo "      $0 --kill"
    echo "      $0 --remove"
}

fun_do_config() {
    work_dir="$HOME"

    # 获取脚本所在目录
    current_dir=$(
        cd "$(dirname "$0")"
        pwd
    )

    mkdir -p $work_dir/$container_env/scripts
    mkdir -p $work_dir/$container_env/package
    mkdir -p $work_dir/$container_env/welog
    mkdir -p $work_dir/$container_env/awe_auto/config
    mkdir -p $work_dir/$container_env/awe_auto/scripts
    mkdir -p $work_dir/$container_env/roadside
    mkdir -p $work_dir/$container_env/roadside_app
    mkdir -p $work_dir/$container_env/roadside_qt/conftg

    # 将脚本拷贝到工作目录
    cp -rf $current_dir/* $work_dir/$container_env/scripts/.

    echo "config dir: $work_dir/$container_env"
}

fun_do_run() {
    shift 2 # 除去前两个变量 -r --
    docker_run_arg=" "
    if [ " $@" != " " ]; then
        docker_run_arg=$(echo $@ | sed 's/+/-/g')
    fi

    ret="$(docker ps -a | grep "$name")"
    if [ " $ret" == " " ]; then
        info "start run [$name] docker container"
        fun_do_config

        # 允许本地访问xhost +
        xhost +

        docker run -it --privileged \
            --net=host \
            --ipc=host \
            --pid=host \
            --gpus all \
            --runtime nvidia \
            --platform linux/arm64 \
            --name=$name \
            -e DISPLAY=$DISPLAY \
            -v /tmp/.X11-unix:/tmp/.X11-unix \
            -v /run/dbus/system_bus_socket:/run/dbus/system_bus_socket:ro \
            -v $work_dir/$container_env/scripts:/root/scripts \
            -v $work_dir/$container_env/package:/root/package \
            -v ~/Documents:/root/workspace \
            -v $work_dir/$container_env/welog:/root/.welog \
            -v $work_dir/$container_env/awe_auto:/root/.awe_auto \
            -v $work_dir/$container_env/roadside:/root/.roadside \
            -v $work_dir/$container_env/roadside_qt:/roott/.roadside_qt \
            -v $work_dir/$container_env/roadside_app:/root/.roadside_app \
            $docker_run_arg \
            -d $docker_image
            # /bin/bash
    else
        info "start run [$name] docker container"
        docker start $name
    fi
}

fun_do_kill() {
    shift 2 # 除去前两个变量 -r --
    kill_name=$name
    if [ " $@" != " " ]; then
        kill_name=$@
    fi

    ret="$(docker ps -a | grep "$kill_name")"
    if [ " $ret" == " " ]; then
        error "can not find [$kill_name] docker container"
        exit -1
    fi

    info "kill [$kill_name] docker container"
    docker kill $kill_name
}

fun_do_exec() {
    shift 2 # 除去前两个变量 -r --
    exec_name=$name
    if [ " $@" != " " ]; then
        exec_name=$@
    fi

    ret="$(docker ps -a | grep "$exec_name")"
    if [ " $ret" == " " ]; then
        fun_do_run
    fi

    info "exec [$exec_name] docker container"
    docker exec -it $exec_name /bin/zsh
    # CPID=$(docker inspect --format "{{.State.Pid}}" $CNAME)
    # nsenter --target "$CPID" --mount --uts --ipc --net --pid
}

fun_do_remove() {
    shift 2 # 除去前两个变量 -r --
    remove_name=$name
    if [ " $@" != " " ]; then
        remove_name=$@
    fi

    ret="$(docker ps -a | grep "$remove_name")"
    if [ " $ret" == " " ]; then
        error "can not find $remove_name docker container"
        exit -1
    fi

    info "remove [$remove_name] docker container"
    docker rm $remove_name
}

fun_do_stop() {
    shift 2 # 除去前两个变量 -r --
    stop_name=$name
    if [ " $@" != " " ]; then
        stop_name=$@
    fi

    ret="$(docker ps -a | grep "$stop_name")"
    if [ " $ret" == " " ]; then
        error "can not find $stop_name docker container"
        exit -1
    fi

    info "stop [$stop_name] docker container"
    docker stop $stop_name
}

fun_do_restart() {
    shift 2 # 除去前两个变量 -r --
    restart_name=$name
    if [ " $@" != " " ]; then
        restart_name=$@
    fi

    ret="$(docker ps -a | grep "$restart_name")"
    if [ " $ret" == " " ]; then
        error "can not find $restart_name docker container"
        exit -1
    fi

    info "restart [$restart_name] docker container"
    docker restart $restart_name
}

main() {
    ARGS=$(getopt -o h,r::,k::,e::,s:: --long help,run::,kill::,exec::,stop::,restart,remove:: -- "$@")
    if [ $? != 0 ]; then
        echo "Terminating..." >&2
        exit 1
    fi
    eval set -- "$ARGS"

    while true; do
        case "$1" in
        -h | --help)
            fun_do_help
            exit 0
            ;;
        -r | --run)
            fun_do_run $@
            exit 0
            ;;
        -k | --kill)
            fun_do_kill $@
            exit 0
            ;;
        -s | --stop)
            fun_do_stop $@
            exit 0
            ;;
        -e | --exec)
            fun_do_exec $@
            exit 0
            ;;
        --remove)
            fun_do_stop $@
            fun_do_remove $@
            exit 0
            ;;
        --restart)
            fun_do_restart $@
            exit 0
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "unknow : {$1}"
            exit 1
            ;;
        esac
    done

}

main "$@"
